package cn.atimer.sdk.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.atimer.sdk.emmus.AtimerCalendar;
import cn.atimer.sdk.emmus.AtimerClient;
import cn.atimer.sdk.emmus.AtimerEvent;
import cn.atimer.sdk.result.CalendarResult;
import cn.atimer.sdk.result.CalendarsResult;
import cn.atimer.sdk.result.DeleteCalendarResult;
import cn.atimer.sdk.result.DeleteEventResult;
import cn.atimer.sdk.result.EventResult;
import cn.atimer.sdk.result.EventsResult;
import cn.atimer.sdk.result.PutCalendarResult;
import cn.atimer.sdk.result.PutEventResult;
import cn.atimer.sdk.result.ViewResult;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;


public class AtimerUtils {
	
	/**=============视图开始==============*/
	/**
	 * 	[用户视图]获取/查询
	 * */
	public static ViewResult getView(AtimerClient client) {
		ViewResult result = new ViewResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.getview;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), ViewResult.class);
		return result;
	}
	
	/**
	 * 	[用户视图]更新
	 * */
	public static ViewResult updateView(AtimerClient client,List<String> calendarIds,List<String> widgets) {
		ViewResult result = new ViewResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarIds", calendarIds);
		paramsMap.put("Widgets", widgets);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.updateview;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), ViewResult.class);
		return result;
	}
	
	/**=============视图结束==============*/
	
	
	/**=============主题开始==============*/
	/**
	 * 	[主题]获取/查询所有
	 * */
	public static CalendarsResult getCalendars(AtimerClient client) {
		CalendarsResult result = new CalendarsResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.getcalendars;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), CalendarsResult.class);
		return result;
	}
	
	/**
	 * 	[主题]获取/查询单个详情
	 * */
	public static CalendarResult getCalendar(AtimerClient client,String calendarId) {
		CalendarResult result = new CalendarResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarId", calendarId);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.getsharedcalendar;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), CalendarResult.class);
		return result;
	}
	
	/**
	 * 	[主题]创建/更新
	 * */
	public static PutCalendarResult putCalendar(AtimerClient client,AtimerCalendar calendar) {
		PutCalendarResult result = new PutCalendarResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("Calendar", calendar);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.putcalendar;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), PutCalendarResult.class);
		return result;
	}
	
	/**
	 * 	[主题]删除
	 * */
	public static DeleteCalendarResult deleteCalendar(AtimerClient client,String calendarId) {
		DeleteCalendarResult result = new DeleteCalendarResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarId", calendarId);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.deletecalendar;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), DeleteCalendarResult.class);
		return result;
	}
	
	/**=============主题结束==============*/
	
	/**=============事件开始==============*/
	/**
	 * 	[事件]获取/查询
	 * */
	public static EventsResult getEvents(AtimerClient client,String calendarId,String from,String to,String offset) {
		//offset为空时，默认8
		if(StrUtil.hasBlank(offset)) {
			offset = "8";
		}
		//from 和 to均为空时，默认当前日期至之后7天
		if(StrUtil.hasBlank(from) && StrUtil.hasBlank(to)) {
			Date date = DateUtil.date();
			from  = DateUtil.format(DateUtil.beginOfDay(date), "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
			to = DateUtil.format(DateUtil.endOfDay(DateUtil.offset(date, DateField.DAY_OF_MONTH, 7)), "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
		}
		// to为空时，默认选择日期至之后7天
		if(!StrUtil.hasBlank(from) && StrUtil.hasBlank(to)) {
			Date dateFrom = DateUtil.parse(from, "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
			to = DateUtil.format(DateUtil.endOfDay(DateUtil.offset(dateFrom, DateField.DAY_OF_MONTH, 7)), "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
		}
		// from为空时，默认选择日期至之前7天
		if(StrUtil.hasBlank(from) && !StrUtil.hasBlank(to)) {
			Date dateTo = DateUtil.parse(to, "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
			from = DateUtil.format(DateUtil.beginOfDay(DateUtil.offset(dateTo, DateField.DAY_OF_MONTH, -7)), "yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
		}
		EventsResult result = new EventsResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarId", calendarId);
		paramsMap.put("From", from);
		paramsMap.put("To", to);
		paramsMap.put("Offset", offset);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.getevents;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), EventsResult.class);
		return result;
	}
	
	/**
	 * 	[事件]获取/查询 单个
	 * */
	public static EventResult getEvent(AtimerClient client,String calendarId,String eventId) {
		EventResult result = new EventResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarId", calendarId);
		paramsMap.put("EventId", eventId);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.getevent;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), EventResult.class);
		return result;
	}
	
	/**
	 * 	[事件]创建/更新
	 * */
	public static PutEventResult putEvent(AtimerClient client,AtimerEvent event) {
		PutEventResult result = new PutEventResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("Event", event);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.putevent;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), PutEventResult.class);
		return result;
	}
	
	/**
	 * 	[事件]删除
	 * */
	public static DeleteEventResult deleteEvent(AtimerClient client,String calendarId,String eventId) {
		DeleteEventResult result = new DeleteEventResult();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("Client", client);
		paramsMap.put("CalendarId", calendarId);
		paramsMap.put("EventId", eventId);
		String path = AtimerConstants.baseDomain + AtimerConstants.baseVersion + AtimerConstants.baseFunction + AtimerConstants.deleteevent;
		result = JSONUtil.toBean(HttpRequest.post(path).body(JSONUtil.toJsonStr(paramsMap)).execute().body(), DeleteEventResult.class);
		return result;
	}
	
	/**=============事件结束==============*/
}
